import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入

@Component({
	selector: 'sa-outsource',
	templateUrl: './outsource.component.html',
})
export class OutsourceComponent implements OnInit {
	tableData:any = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	btnData:any = [];
	outsourceAdd:boolean = false;
	outsourceEdit:boolean = false;
	outsourceDeleteBatch:boolean = false;
	
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage || 1;
		});
	}

	ngOnInit() {
		this.initBtn();
		this.load();
	}
	//table刷新
    refresh(){
    	this.load();
    }
    //权限按钮
	initBtn(){
		this.btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_system:outsource')));
		if(!this.btnData){return};
		for(let item of this.btnData){
			if(item.sourceKey === "outsource:add"&&item.parentId){this.outsourceAdd = true;}
			if(item.sourceKey === "outsource:edit"&&item.parentId){this.outsourceEdit = true;}
			if(item.sourceKey === "outsource:deleteBatch"&&item.parentId){this.outsourceDeleteBatch = true;}
		}
	}
	//表格搜索
	table_search(){
		this.curPage = 1
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//初始化加载list数据
	load(){
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				searchName:this.searchKey
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("outsourcingTeam/queryOutsourcingTeam.html", reqdata).subscribe(data => {
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
		})
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//新建公司
	new_company(){
		this.router.navigate(['/system/outsource/outsource-page'], {queryParams: {type: 'new'}});
	}
	//编辑页面
	updata_company(row){
		this.router.navigate(['/system/outsource/outsource-page'], {queryParams: {id: row.id,type: 'updata',nowPage:this.curPage}});
	}
	//公司删除
	del_company(row){
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			let reqdata = 'ids=' + row.id;
			this.jsonApiService.http("outsourcingTeam/deleteOutsourcingTeam.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if(data.success){
					this.load();
				}
			})
		})
	}
}